package messages;

import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import utils.Serialization;
import utils.XmlSerializationUtils;
import connection.exception.ConnectionResetException;

public class DocumentSigningRequestMessage implements Convertible {

    private static final long serialVersionUID = 1L;

    public Integer _nonce;
    
    public String _nonceSignature;
    
    public String _competence;
    
    public String _file;
    
    public Integer _duration;
    
    public Boolean _hasPriority;

    public DocumentSigningRequestMessage() {}
     
    public DocumentSigningRequestMessage(Integer nonce, String signature, String competence, String file,
            Integer duration, Boolean priority) {
        _nonce = nonce;
        _nonceSignature = signature;
        _competence = competence;
        _file = file;
        _duration = duration;
        _hasPriority = priority;
    }
    
    @Override
    public Element convertToCanonic(Document doc) {
        
        String className = getClass().getSimpleName();
        
        Element el = doc.createElement(className);

        Element childEl2 = doc.createElement("Nonce");
        childEl2.setTextContent(_nonce.toString());
        el.appendChild(childEl2);
        
        if (_nonceSignature != null) {
            Element childEl = doc.createElement("NonceSignature");
            childEl.setTextContent(_nonceSignature);
            el.appendChild(childEl);

        }
        
        if (_competence != null) {
            Element childEl = doc.createElement("Competence");
            childEl.setTextContent(_competence);
            el.appendChild(childEl);

        }
        
        if (_file != null) {
            Element childEl = doc.createElement("File");
            childEl.setTextContent(_file);
            el.appendChild(childEl);

        }
        
        Element childEl3 = doc.createElement("Duration");
        childEl3.setTextContent(_duration.toString());
        el.appendChild(childEl3);
        
        if (_hasPriority != null) {
            Element childEl = doc.createElement("HasPriority");
            childEl.setTextContent(_hasPriority.toString());
            el.appendChild(childEl);
        }

        return el;
    } 


    @Override
    public Object convertFromCanonic(Element el) {
        this._nonce = XmlSerializationUtils.getIntValue(el, "Nonce");
        this._nonceSignature = XmlSerializationUtils.getTextValue(el, "NonceSignature");
        this._competence = XmlSerializationUtils.getTextValue(el, "Competence");
        this._file = XmlSerializationUtils.getTextValue(el, "File");
        this._duration = XmlSerializationUtils.getIntValue(el, "Duration");
        this._hasPriority = new Boolean(XmlSerializationUtils.getTextValue(el, "HasPriority"));
        return this;
    }

    public static void main(String[] args) throws ConnectionResetException, IOException{

        DocumentSigningRequestMessage gen = new DocumentSigningRequestMessage();
        gen._nonce = 3;
        gen._nonceSignature = "SFDAFECcasdasdas";
        gen._competence = "PresidenteIST";
        gen._file = "DSADASDASDA";
        gen._duration = 32;
        gen._hasPriority = false;

        byte[] serialized = Serialization.Serialize(gen);

        Object newGen = Serialization.Deserialize(serialized);

        newGen.toString();

        System.in.read();
    }
}

